<?php

class Kd_Html_Form extends Kd_Html_Element {

	private $_title = '';
    private $_fields = array();
    private $_hiddens = array();
    private $_tabs = array();

    public function __construct() {
        parent::__construct();
        $this->templateName = 'form';
        $this->setSetting('validate', TRUE)
                ->setSetting('ajax', FALSE)
                ->setMethod('post');
    }
    
    public function getTitle(){
    	return $this->_title;
    }
    
    public function setTitle($value){
    	$this->_title = $value;
    	return $this;
    }    

    public function setEnctype($value) {
        return $this->setAttribute('enctype', $value);
    }

    public function getEnctype() {
        return $this->getAttribute('enctype');
    }

    public function getAction() {
        return $this->getAttribute('action');
    }

    public function setAction($value) {
        $this->setAttribute('action', $value);
        return $this;
    }

    public function getMethod() {
        return $this->getAttributes('method');
    }

    public function setMethod($value) {
        $this->setAttribute('method', $value);
        return $this;
    }

    public function createTab($id, $label, array $fields){
        $result = new Kd_Html_Form_Tab();
        $result->setId($id)
               ->setLabel($label)
               ->addField($fields);
        $this->_tabs[] = $result;
        return $result;
    }

    public function getTabs(){
        return $this->_tabs;
    }

    public function getField($id) {
        foreach ($this->getFields() as $field) {
            if ($field->getId() == $id) {
                return $field;
            }
        }
        return NULL;
    }

    public function getFields() {
        return $this->_fields;
    }

    public function createField($label = '', $required = FALSE, $id = NULL) {
        $result = new Kd_Html_Form_Field();
        $result->setLabel($label)->setRequired($required);
        if (!is_null($id)) {
            $result->setId($id);
        }
        $this->addField($result);
        return $result;
    }

    public function addField($field) {
        $this->_fields[] = $field->setForm($this);
        return $this;
    }

    public function removeField() {
        $args = func_get_args();
        while (count($args) > 0) {
            $arg = array_shift($args);
            for ($i = 0; $i < count($this->_fields); $i++) {
            	if(isset($this->_fields[$i])){
	                if ($this->_fields[$i]->getId() == $arg) {
	                    unset($this->_fields[$i]);
	                    break;
	                }
            	}
            }
        }
        return $this;
    }   

    public function bindData(array $values = array(), $inputIds = NULL, $hiddenIds = NULL) {
        foreach ($this->_fields as $field) {
            $id = $field->getInput()->getId();

            if (is_null($inputIds) || in_array($id, $inputIds)) {
                if (array_key_exists($id, $values)) {
                    $field->getInput()->setValue($values[$id]);
                }
            }
        }

        foreach ($this->_hiddens as $hidden) {
            $id = $hidden->getId();
            if (is_null($hiddenIds) || in_array($id, $hiddenIds)) {
                if (array_key_exists($id, $values)) {
                    $hidden->setValue($values[$id]);
                }
            }
        }

        return $this;
    }

    public function getInput($inputId) {
        foreach ($this->_fields as $field) {
            if ($field->getInput()->getId() == $inputId) {
                return $field->getInput();
            }
        }

        return NULL;
    }

    public function getHiddens() {
        return $this->_hiddens;
    }

    public function addTextboxField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('textbox', $id, $name);
    }

    public function addValidateCodeField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('validateCode', $id, $name);
    }

    public function addTextareaField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('textarea', $id, $name);
    }

    public function addEditorField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('editor', $id, $name);
    }

    public function addPasswordField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('password', $id, $name);
    }

    public function addSelectField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('select', $id, $name);
    }

    public function addBooleanField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('boolean', $id, $name);
    }

    public function addDatetimeField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('datetime', $id, $name);
    }

    public function addCheckListField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('checkList', $id, $name);
    }

    public function addFileField($label, $id, $name, $required = false) {
        $field = $this->createField($label, $required, "field_$id");
        return $field->createInput('file', $id, $name);
    }

    public function addHiddenField($id, $name, $value = '') {
        $result = new Kd_Html_Form_Hidden();
        $result->setId($id)->setName($name)->setValue($value);
        $this->_hiddens[$id] = $result;
        return $this;
    }

    protected function render() {
        $result = '<form ' . $this->getAttributeHtml() . '>';
        $result .= '<table><tbody>';
        foreach ($this->getFields() as $field) {
            $result .= $field->toHtml();
        }
        $result .= '</tbody></table>';
        foreach ($this->_hiddens as $hidden) {
            $result .= $hidden->toHtml();
        }
        $result .= '</form>';
    }

}